package tw.cuttle.entry;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import tw.cuttle.CuttleContext;
import tw.cuttle.bean.Actions;

/**
 * Servlet Filter implementation class RequestFilter
 */
public class RequestFilter implements Filter {
    private static Log LOGGER=LogFactory.getLog(RequestFilter.class);
    private EntryHelper entryHelper=new EntryHelper();

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
	    	entryHelper.execute((HttpServletRequest)request, (HttpServletResponse)response);
	}
	
	
	
	/**
	 * @see Filter#init(FilterConfig)
	 * initial cuttle's beanHolder
	 */
	public void init(FilterConfig fConfig) throws ServletException {
	    try {
		String surfix=fConfig.getInitParameter("surfix");
			if(surfix!=null)this.entryHelper.setSurfix(surfix);
		entryHelper.init(fConfig.getServletContext());
		WebApplicationContext wac=WebApplicationContextUtils.getWebApplicationContext(fConfig.getServletContext());
	    }catch(Exception e){
		LOGGER.error("RequestFilter init failed!!");
		throw new RuntimeException(e);
	    }
	    	
	}
	

}
